package cn.itsource.hrm.util;

import cn.itsource.hrm.domain.Sso;
import cn.itsource.hrm.jwt.JwtUtils;
import cn.itsource.hrm.jwt.Payload;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
 * 用户上下文：和登录用户相关的东西都封装到这儿
 *  1 登录
 *  2 权限。。。。
 */
public class UserContext {

    /**
     * 获取当前登录用户
     * @param request
     * @return
     */
    public static Sso currentSso(HttpServletRequest request){
        String jwtToken = request.getHeader("jwtToken");
        PublicKey publicKey = RsaUtils.getPublicKey(UserContext.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile());
        Payload<Sso> payload = JwtUtils.getInfoFromToken(jwtToken, publicKey, Sso.class);
        Sso sso = payload.getUserInfo();
        return  sso;
    }
}
